/*
 * Copyright (c) [2020] Zhang Yansen.All rights reserved.
 *
 * with-function is licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

/**
 * useList 模拟 hook 方式进行 List 添加
 */
export function useList() {
  let list = []

  function append(fn) {
    const len = list.length
    list.push(fn)

    return () => list.splice(len, 1)
  }

  return [list, append]
}

/**
 * compose 与 FP 编程 中的 compose 概念有点差别
 * 此处仅仅是遍历执行, 另外对函数进行了 handler 绑定操作
 * 
 * @param {*} fnList 
 * @param {*} handler 
 */
export function compose(fnList = [], handler) {
  return (...args) => {
    fnList.forEach(f => f.call(handler, ...args))
  }
}
